/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'groupbuys',
                condition: {},
                model: {
                    url: _global.url.api + 'groupbuy',
                    event: {
                        '.item-remove:click': 'remove',
                        '.item-edit:click': 'edit'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var itemObj = $('.remove-' + _this.data.id).parent().parent();
                        dialog({
                            content: '确定要删除吗？',
                            ok: function () {
                                _this.data.action = 'del';

                                if (!_this.put()) {
                                    return false;
                                }
                                itemObj.fadeOut("slow");
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            title: '编辑：' + _this.data.name,
                            width: 600,
                            content: template('edit-tpl', _this.data),
                            ok: function () {
                                _this.data.action = 'edit';
                                _this.data.listorder = $('#listorder').val();
                                _this.data.pro_started_at = $('#pro_started_at').val();
                                _this.data.pro_expired_at = $('#pro_expired_at').val();
                                _this.data.status = $('input[name="status"]:checked').val();
                                if (!_this.put()) {
                                    return false;
                                }
                                //that.collection.reload();
                            }, cancel: function () {
                            }
                        }).show();
                        //日期
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $('input[name="keyword"]').val('');
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

        }
    };

    new index();

});